package com.typhoon.extend.controller;

import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.typhoon.extend.constant.ExtendEntityError;
import com.typhoon.extend.dto.AccountDto;
import com.typhoon.extend.dto.ResponseDto;
import com.typhoon.extend.dto.request.LoginReq;
import com.typhoon.extend.service.AccountService;
import com.typhoon.extend.util.CommonUtils;

@Controller
@RequestMapping("/login")
public class LoginController extends BaseController {
	
	private static final Logger LOGGER = LoggerFactory.getLogger(LoginController.class);
	
	@Resource
	AccountService accountService;
	
	@RequestMapping(value = "/tologin.do",method = RequestMethod.POST)
	public void doLogin(@RequestBody Map<String,Object> params, HttpServletResponse response) {
		
		String accountCode = CommonUtils.evalString(params.get("accountCode"));
		if(StringUtils.isEmpty(accountCode)) {
			this.putParamResult(response, ExtendEntityError.ILLEGAL_ARGUMENT_ERROR);
			return;
		}
		
		String password = CommonUtils.evalString(params.get("password"));
		if(StringUtils.isEmpty(password)) {
			this.putParamResult(response, ExtendEntityError.ILLEGAL_ARGUMENT_ERROR);
			return;
		}
		LoginReq req = new LoginReq();
		req.setAccountCode(accountCode);
		req.setPassword(password);
		try {
			ResponseDto<AccountDto> resp = this.accountService.doLogin(req);
			if(null == resp || !resp.isSuccess()) {
				this.putFailtResult(response,"登录失败");
				return ;
			}
			//登录成功
			this.putSuccessResult(response,resp.getDto());
		} catch (Exception e) {
			LOGGER.error("登录异常",e);
			
		}
		
	}

}
